package com.joey.concurrency.example.commUnsafe;

import com.joey.concurrency.annotation.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @Author hyy
 **/
@Slf4j
@NotThreadSafe
public class SimpleDateFormatExample {

    private static SimpleDateFormat simpleDateFormat
            = new SimpleDateFormat("yyyy-MM-dd");

    public static int clientCount = 1000;
    public static int threadTotal = 200;
    public static void main(String[] args) throws Exception{
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientCount);
        for(int i=0;i<clientCount;i++){
            executorService.execute(() -> {
                try {
                    //判断当前进程是否被执行
                    semaphore.acquire();
                    update();
                    semaphore.release();
                }catch (Exception e){
                    log.error("exception",e);
                }
                countDownLatch.countDown();

            });
        }

        countDownLatch.await();
        executorService.shutdown();
    }

    public static void update(){
        //Volatile不具有原子性
        try {
            simpleDateFormat.parse("2018-11-11");
        } catch (ParseException e) {
            log.error("parseException",e);
            e.printStackTrace();
        }
        //1.count
        //2.+1
        //3.count

    }

}
